package com.yiqi.lottery.feature.find.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.CircleBo;
import com.yiqi.lottery.entity.bo.FoucsBo;
import com.yiqi.lottery.feature.find.contract.CircleContract;
import com.yiqi.lottery.feature.find.dependencies.circle.CirclePresenterModule;
import com.yiqi.lottery.feature.find.dependencies.circle.DaggerCircleComponent;
import com.yiqi.lottery.feature.find.view.adapter.CircleAdapter;

import javax.inject.Inject;

public class OrderShareCircleActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener,
        RadioGroup.OnCheckedChangeListener, CircleContract.View, CircleAdapter.GetCheckedValue, BaseQuickAdapter.RequestLoadMoreListener {


    @Inject
    CircleContract.Presenter mPresenter;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_find_circle)
    RecyclerView mRvFindCircle;
    @BindView(R.id.rgb_find_circle_jinhua)
    RadioButton mRgbFindCircleJinhua;
    @BindView(R.id.rgb_find_circle_all)
    RadioButton mRgbFindCircleAll;
    @BindView(R.id.rgb_find_circle_my)
    RadioButton mRgbFindCircleMy;
    @BindView(R.id.rgp_find_circle)
    RadioGroup mRgpFindCircle;
    @BindView(R.id.iv_xiala)
    ImageView mIvXiala;
    @BindView(R.id.tv_circlr_rule)
    TextView mTvCirclrRule;
    private CircleAdapter mAdapter;
    private EmptyView mEmptyView;

    PageData<CircleBo> mData;
    private int pageSize = 10;
    private int mCurrentCounter = 0;
    private int pageIndex = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private boolean isShowRule = false;
    private int mType = 1;

    int mPos;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order_share_circle;
    }

    @Override
    protected void initInjector() {
        DaggerCircleComponent.builder().circlePresenterModule(new CirclePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        mToolbar.inflateMenu(R.menu.menu_find_circle_add);
        initToolBar(mToolbar, "晒单圈", true, true);
        mToolbar.setOnMenuItemClickListener(this);
        initAdapter();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void initData() {
        mPresenter.doGetCircleList(0, 1, mType);
    }

    private void initAdapter() {
        mAdapter = new CircleAdapter(this);
        mEmptyView = new EmptyView(mRvFindCircle);
        mRvFindCircle.setLayoutManager(new LinearLayoutManager(this));
        mRvFindCircle.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(25));
        mAdapter.bindToRecyclerView(mRvFindCircle);
        mAdapter.setOnItemChecked(this);
        mAdapter.setOnLoadMoreListener(this);
    }


    @Override
    protected void initEvent() {
        mRgpFindCircle.setOnCheckedChangeListener(this);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:
                startActivity(new Intent(this, WriteCircleActivity.class));
                break;
        }
        return true;

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        pageIndex = 0;
        if (checkedId == R.id.rgb_find_circle_all) {
            mType = 2;
            mRgbFindCircleJinhua.setTextColor(getResources().getColor(R.color.text_primary));
            mPresenter.doGetCircleList(pageIndex, 1, mType);
        } else if (checkedId == R.id.rgb_find_circle_my) {
            mType = 3;
            mRgbFindCircleJinhua.setTextColor(getResources().getColor(R.color.text_primary));
            mPresenter.getMyCircleList(pageIndex);

        } else if (checkedId == R.id.rgb_find_circle_jinhua) {
            mType = 1;
            mPresenter.doGetCircleList(pageIndex, 1, mType);
            mRgbFindCircleJinhua.setTextColor(getResources().getColor(R.color.primary_highlight));
        }
    }


    @Override
    public void showCircleListSuccess(PageData<CircleBo> data) {
        mData = data;
        mAdapter.setNewData(mData.getList());
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showCircleListMoreSuccess(PageData<CircleBo> circleBoPageData) {
        isErr = false;
        mAdapter.addData(circleBoPageData.getList());
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showCircleMoreErrorMsg(String msg) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showDetailSuccess(FoucsBo foucsBo) {

    }

    @Override
    public void showCircleListError(String error) {
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
    }

    @Override
    public void showCircleListEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView("暂无晒单数据！"));
        mAdapter.setNewData(null);
    }

    @Override
    public void showDaiaZanSuccess(String msg) {
//      mAdapter.notifyItemChanged(mPos);
    }

    @Override
    public void showDaiaZanError(String msg) {
        T.showShort(msg);
    }

    @Override
    public void getValue(boolean stauts, Integer ids, int pos) {
        mPos = pos;
        if (stauts) {
            mPresenter.dianZan(ids);
        } else {
            mPresenter.cancleDianzan(ids);
        }

    }

    @Override
    public void onLoadMoreRequested() {
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd(true);
        } else {

            if (mCurrentCounter >= mData.getTotal()) {
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            } else {
                if (!isErr) {
                    pageIndex++;
                    if (mType < 3) {
                        mPresenter.doGetCircleMoreList(pageIndex, 1, mType);
                    } else {
                        mPresenter.getMyCircleMoreList(pageIndex);
                    }
                }
            }
        }
    }

    @OnClick(R.id.iv_xiala)
    public void onViewClicked() {
        if (!isShowRule) {
            mTvCirclrRule.setVisibility(View.VISIBLE);
            mIvXiala.setImageResource(R.drawable.icon_shang);
            isShowRule = true;
        } else {
            mTvCirclrRule.setVisibility(View.GONE);
            mIvXiala.setImageResource(R.drawable.icon_xiala);
            isShowRule = false;
        }
    }

}
